package com.cookor.poi.modules.phy.schedule;

import com.alibaba.fastjson.JSONObject;
import com.cookor.poi.common.utils.DateUtils;
import com.cookor.poi.modules.phy.entity.TOrder;
import com.cookor.poi.utils.HttpUtils;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//@Service
//@Lazy(false)
public class YurenSchedule {

    private static final Logger logger = Logger.getLogger(ShiftSchedule.class);

    @Scheduled(cron = "0 */1 * * * ?")
    @Transactional(readOnly = false, rollbackFor = {Exception.class})
    public void getJson() throws Exception {
        logger.info("=============越人数据同步=============");
        syncData();
    }

    public static void syncData()throws Exception{
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("mobile","13159166666");
        jsonObject.put("password","166666");
        jsonObject.put("remember_me", false);
        String s = HttpUtils.sendPostRequest("https://health.yrjkjs.com/sys/user/login.do", jsonObject.toJSONString(), null);
        String token = JSONObject.parseObject(s).getJSONObject("body").getString("token");

        System.out.println(token);
        String s1 = HttpUtils.sendPostRequest("https://health.yrjkjs.com/vis/patientOrder/loadPatientOrderList.do?_t=1700206468&column=createTime&order=desc&field=id,,patientModel.name,patientModel.sex,patientModel.mobile," +
                "orderType,orderDate,state,origin,hospitalName,action&page=1&rows=10000", null, token);
        File dir = new File("/mnt/java_site/qxjy/json");
        if(!dir.exists()){
            dir.mkdirs();
        }
        String filePath = "/mnt/java_site/qxjy/json/file.json";
        try {
            // 使用FileWriter写入文件, false表示覆盖
            FileWriter fileWriter = new FileWriter(filePath, false);
            // 使用BufferedWriter包装FileWriter，提高写入性能
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            // 将JSON字符串写入文件
            bufferedWriter.write(s1);
            // 关闭写入流
            bufferedWriter.close();
            System.out.println("JSON字符串成功保存到文件：" + filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static JSONObject getData()throws Exception{
        // 使用FileReader读取文件
        FileReader fileReader = new FileReader("/mnt/java_site/qxjy/json/file.json");
        // 使用BufferedReader包装FileReader，提高读取性能
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        // 用于保存文件内容的StringBuilder
        StringBuilder stringBuilder = new StringBuilder();
        String line;
        // 逐行读取文件内容
        while ((line = bufferedReader.readLine()) != null) {
            stringBuilder.append(line);
        }
        // 关闭读取流
        bufferedReader.close();
        // 将文件内容转换为JSON对象
        return JSONObject.parseObject(stringBuilder.toString());
    }


}
